<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="<%=path%>/hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=path%>/hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
	<link href="<%=path%>/hplus/css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">
    <link href="<%=path%>/hplus/css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/colorpicker/css/bootstrap-colorpicker.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/cropper/cropper.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/switchery/switchery.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/jasny/jasny-bootstrap.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/nouslider/jquery.nouislider.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/ionRangeSlider/ion.rangeSlider.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/ionRangeSlider/ion.rangeSlider.skinFlat.css" rel="stylesheet">
    <link rel="stylesheet" href="<%=path%>/hplus/css/bootstrap-multiselect.css" type="text/css"/>
    <link rel="stylesheet" href="<%=path%>/hplus/css/prettify.css" type="text/css"/>
        <link rel="stylesheet" href="<%=path%>/hplus/css/bootstrap-example.css" type="text/css"/>
   
    <style> 
        .radio label{
            padding-left: 0;
        }

        .date{
            margin: -8px 20px 0 0;
        }

        .clockpicker{
            margin-top: -8px;
        }

        .i-checks label{
            float: left;
            margin-right: 10px;
        }

        .col-sm-offset-1{
            margin-top: 20px;
        }

        .tishi{
            margin-top: 7px;
            
        }

        .alert{
            position:absolute;
            top: -15px;
            left: 8%;
            display:none;
        }
        .text-center{
            width:50%;
            margin: 10px 0 20px;
        }
    </style>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
       <div class="row white-bg" style="padding:10px">
	       <div class="col-md-8" id="boxPage">
	                <div class="form-group">
	                    <label class="col-sm-2 control-label text-right" data-toggle="modal" data-target="#myModal5">商品名称</label>
	                    <div class="col-sm-6">
	                          <input id="mecicine_name" medicineIds=""  type="text" readonly="readonly" class="form-control medicineNme"  data-toggle="modal" href="<%=path%>/ser/ui/medicineModal" data-target="#medicine_modal">
	                    </div>
	                   <%--  <div class="tishi col-sm-3" style="position:relative;margin-left:-15px">
	                        <img src="<%=path%>/bm/images/help.png" />
	                        <div class="col-sm-4 alert alert-success">
	                            	如果没有签名，请联系您的售后人员。<!-- <a class="alert-link" href="notifications.html#">了解更多</a> -->
	                        </div>
	                    </div>   --%>
	                </div>
	       </div>
       
           <div class="col-sm-4 text-right">
              <button onclick="javascript:window.location.href='<%=path%>/ser/ui/automatedOperation';" type="button" class="btn btn-outline btn-primary" style="margin-right:10px">取消</button>
              <button onclick="javascript:saveTaskRule();" type="button" class="btn btn-primary">保存</button>
           </div>    
       </div>
        
    </div>

   


   
    <div class="modal inmodal fade" id="model_modal" tabindex="-1" role="dialog"  aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
               
            </div>
        </div>
    </div>
    <div class="modal inmodal fade" id="medicine_modal" tabindex="-1" role="dialog"  aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
               
            </div>
        </div>
    </div>
     <div class="modal inmodal fade" id="modelEdit_modal" tabindex="-1" role="dialog"  aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
               
            </div>
        </div>
    </div>
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/content.min.js?v=1.0.0"></script>
    <script src="<%=path%>/hplus/js/plugins/chosen/chosen.jquery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jsKnob/jquery.knob.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jasny/jasny-bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/prettyfile/bootstrap-prettyfile.js"></script>
    <script src="<%=path%>/hplus/js/plugins/switchery/switchery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="<%=path%>/hplus/js/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/clockpicker/clockpicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/cropper/cropper.min.js"></script>
    <script src="<%=path%>/hplus/js/demo/form-advanced-demo.min.js"></script>
	<script src="<%=path%>/hplus/js/plugins/suggest/bootstrap-suggest.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
 
	<script type="text/javascript" src="<%=path%>/js/marketing/dialog.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/lengthlimit.js"></script>
    <script type="text/javascript" src="<%=path%>/hplus/js/bootstrap-multiselect.js"></script>
    <script type="text/javascript" src="<%=path%>/hplus/js/prettify.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/public.js"></script>
	<script type="text/javascript">
		function refreshContent(){
		}
	
		 $(document).on('click', '.divcss5', function() {
			 var rule_id=$(this).attr('rule_id');
			 var medicine_name=$('#mecicine_name').val();
			 var buy_day=$('#buyDay'+rule_id+'').val();
			 var model_content=$('#ccomment'+rule_id+'').val();
			 if(StringEmpty(medicine_name)&&StringEmpty(buy_day)&&StringEmpty(model_content)){
				 rule_id=rule_id+1;
				 ruleHtml(rule_id,"",true,"create");
				 $(this).hide();
			 }else if(!StringEmpty(medicine_name)){
				 errorDialog("请选择商品");
			 }else if(!StringEmpty(buy_day)){
				 errorDialog("输入购买天数");
			 }else if(!StringEmpty(model_content)){
				 errorDialog("设置模板信息");
			 }
			
		 });
		 
		 $(document).on('click', '.ruleId', function() {
			 ruleIdTemp=$(this).attr("rule_id");
			 modelIdTemp=$(this).attr("modelId");
		 });
		 
		
		 function determineModel(){
	        	$(".select_one .checked .childCheckbox1").each(
	        			function(i, d) {
	        				var modelContent = $(this).attr("modelContent");
	        				$('#ccomment'+ruleIdTemp+'').val(modelContent);
	        				$('#ccomment'+ruleIdTemp+'').attr('modelId',this.id);
	        				$('#definedModel'+ruleIdTemp+'').attr('modelId',this.id);
	        	});
	        	$('#model_modal').modal('hide');
	        }
	      
	        function colseModelModal(){
	        	$('#model_modal').modal('hide');
	        }
	        
	        
	        function determineMedicine(){
	        	$('#mecicine_name').val(medicineNames.join(","));
	        	$('#mecicine_name').attr('medicineIds',medicineIds.join(","));
	        	$('#medicine_modal').modal('hide');
	        	console.log("----->"+medicineIds.join(","));
	        }
	      
	        function colseMedicineModal(){
	        	$('#medicine_modal').modal('hide');
	        }
	        function determineEditModel(){
	        	 $('#ccomment'+ruleIdTemp+'').val(modelContentTemp);
				$('#ccomment'+ruleIdTemp+'').attr('modelId',modelIdTemp); 
	        	$('#modelEdit_modal').modal('hide');
	        }
	      
	        function colseEditModel(){
	        	$('#modelEdit_modal').modal('toggle');
	        }
	        
		 
	</script>
	
	
    <script>
        //$(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
       
       var access_token=$.cookie('access_token');
   	   var cid=$.cookie('cid');
   	   var uid=$.cookie('uid');
        
       var modelName;
       var modelType;
       var contentType;
       var content;
       var autograph_id;
       var taskId;
       var modelId=0;
       var medicineIds=new Array();
   	   var medicineNames=new Array();
       var medicineIdsMap=new Map();
       var medicineNamesMap=new Map();
       //操作类型
       var type;
       var ruleIdTemp;
       var modelIdTemp;
       var modelContentTemp;
        
        $(function(){
        	        	taskId=getQueryString("id");
            type=getQueryString("type");
            if(type=="edit"){
            	getDetail();
            }else{
            	ruleHtml(1,"",true,"create");
            }
           
        })
        
        
        function getDetail(){
        	var url="<%=path%>/api/ser/detail";
    		$.post(url, {
    			"access_token" : access_token,
    			"cid" : cid,
    			"uid" : uid,
    			"id" : taskId,
    		}, function(data) {
    			
    			if (data.status == 200) {
    				$('#mecicine_name').val(data.medicineNames);
    				var ruleList=data.ruleList;
    				medicineIds=data.medicineIds.split(",");
    				for(var i in ruleList){
    					console.log(i);
    					if(i!=ruleList.length-1){
    						ruleHtml(ruleList[i].ruleId,ruleList[i],false,"update");
    					}else{
    						ruleHtml(ruleList[i].ruleId,ruleList[i],true,"update");
    					}
    				}
    				//initHtml(data.data);
    			}else if(data.error_code=="20028"){
	        		loginDialog();
	        	}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
       
        //初始化页面
        function initHtml(data){
        	
        }
        
        
        //获取标签列表
        function getAutographList(){
        	var url="<%=path%>/smsAutograph/ajaxAutographList";
    		$.post(url, {
    			"access_token" : access_token,
    			"cid" : cid,
    			"uid" : uid,
    			"id" : id,
    		}, function(data) {
    			
    			if (data.status == 200) {
    				setAutographHtml(data.data);
    			}else if(data.error_code=="20028"){
	        		loginDialog();
	        	}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
        
        
        //保存
        function saveTaskRule(){
        	var saveTag=true;
        	var parmList=new Array();
        	$('.eachData').each(function(i,d){
        		
        			var parmObj=new Object();
               		var buyDay=$(d).find(".buyDays").val();
               		var ruleId=$(d).find(".buyDays").attr('ruleId');
               		var saveType=$(d).find(".buyDays").attr('saveType');
               		var sms_model_id=$(d).find(".modelContent").attr('modelId');
               		if(StringEmpty(medicineIds)&&StringEmpty(buyDay)&&StringEmpty(sms_model_id)){
                   		parmObj.buyDay=buyDay;
                   		parmObj.ruleId=ruleId;
                   		parmObj.sms_model_id=sms_model_id;
                   		parmObj.saveType=saveType;
                   		parmObj.is_deleted=0;
                   		parmList.push(parmObj);
               		}else if( !StringEmpty(medicineIds)){
               			promptDialog("请选择商品");
               			saveTag=false;
               			return false;
               		}else if(!StringEmpty(buyDay)){
               			promptDialog("设置购买天数");
               			saveTag=false;
               			return false;
               		}else if(!StringEmpty(sms_model_id)){
               			promptDialog("设置短信模板信息");
               			saveTag=false;
               			return false;
               		}
           	});
        	
        	if(saveTag){
        		if(type=="create"){
        			createTask(parmList);
        		}else{
        			updateTask(parmList);
        		}
        	}
        	
        }
        function createTask(parmData){
        	var ruleObj= JSON.stringify(parmData);
        	var medicineIds2=medicineIds.join(",");
        	console.log("medicineIds--->"+medicineIds);
        	var url="<%=path%>/api/ser/create";
    		$.post(url, {
    			"access_token" : access_token,
    			"cid" : cid,
    			"uid" : uid,
    			"ruleObj" : ruleObj,
    			"medicineIds":medicineIds2,
    		}, function(data) {
    			
    			if (data.status == 200) {
    				OKDialog1("保存成功","");
    			}else if(data.error_code=="20028"){
	        		loginDialog();
	        	}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
        
        function updateTask(parmData){
        	var ruleObj= JSON.stringify(parmData);
        	var medicineIds2=medicineIds.join(",");
        	console.log("medicineIds--->"+ruleObj);
        	var url="<%=path%>/api/ser/update";
    		$.post(url, {
    			"access_token" : access_token,
    			"cid" : cid,
    			"uid" : uid,
    			"ruleObj" : ruleObj,
    			"medicineIds":medicineIds2,
    			"taskId":taskId,
    		}, function(data) {
    			
    			console.log("data---->"+data.error_code);
    			if (data.status == 200) {
    				OKDialog1("保存成功","");
    			}else if(data.error_code=="20028"){
	        		loginDialog();
	        	}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
        
        
        
       
      
        
        
        function ruleHtml(rule_id,ruleData,addFlag,saveType){
        	var html='';
        	html+='<div  class="ibox-content" style="border-color:#fff">';
            html+='<form method="get" class="form-horizontal eachData">';
                  
                html+='<div class="form-group">';
                    html+='<label class="col-sm-2 control-label">购买天数</label>';
                    html+='<div class="col-sm-6">';
                        html+='<input ruleId="'+rule_id+'" saveType="'+saveType+'" id="buyDay'+rule_id+'" value="'+(StringEmpty(ruleData.after_days) ? ruleData.after_days:"0" )+'" type="text" class="form-control buyDays"  onkeypress="keyPress()" maxlength="3">';
                    html+='</div>';
                    html+='<label class="col-sm-1 text-left" style="margin-top: 8px;margin-left: -13px;">天</label>';
                    html+='<a class="deleteRule col-sm-1"><img style="width:30px;height:30px;margin-top:3px;" src="<%=path%>/images/marketing/icon_delect.png"/></a>';
                html+='</div>';
                 html+='<div class="form-group">';
                    html+='<label class="col-sm-2 control-label" data-toggle="modal" data-target="#myModal5">短信签名</label>';
                    html+='<div class="col-sm-6">';
                        html+='<div class="i-checks">';
                            html+='<label style="float:left;margin-right:10px;margin-top:7px;">';
                                html+='<input type="radio"  checked="" value="option2"> <i></i>短信</label>';
                        html+='</div>';
                    html+='</div>';
                html+='</div>';
                html+='<div class="form-group">';
                  html+='<label class="col-sm-2 control-label">模版信息</label>';
                  html+='<div class="col-sm-6 text-right" style="margin-top:8px;">';
	                  html+='<button  type="button" class="btn btn-primary btn-xs  ruleId" rule_id="'+rule_id+'" data-toggle="modal" href="<%=path%>/api/bm/ui/modalSmsModel" data-target="#model_modal" style="margin-right:5px;">选择模板</button>';
	                  html+='<button  id="definedModel'+rule_id+'" type="button" class="btn btn-primary btn-outline btn-xs  ruleId" rule_id="'+rule_id+'" modelId="'+ruleData.modelId+'" data-toggle="modal" href="<%=path%>/ser/ui/modalSmsModelEdit" data-target="#modelEdit_modal" style="margin-right:5px;">自定义模板</button>';
                  html+='</div>';
                html+='</div>';
                html+='<div class="form-group">';
                   html+='<label class="col-sm-2 control-label" style="visibility: hidden">模板内容</label>';
                    html+='<div class="col-sm-6" style="margin-top:-10px;">';                      
                        html+='<textarea  readonly="readonly" oninput="reloadCountWordNum()" modelId="'+(StringEmpty(ruleData.modelId) ? ruleData.modelId:"" )+'" value="'+(StringEmpty(ruleData.content)? ruleData.content:"")+'" id="ccomment'+rule_id+'" name="comment" class="form-control modelContent" required="" aria-required="true" style="height:150px;">'+(StringEmpty(ruleData.content)? ruleData.content:"")+'</textarea>';                    
/* 	                    html+='<div class=" text-right"> ';
	                       html+='<h6 class="col-sm-9 "></h6> <span class="text-right" id="current">70</span><span>/</span><span id="remain" class="text-left">0</span><span>条</span>';
	                    html+='</div>'; */
                    html+='</div>';
                    <%--  html+='<div class="tishi col-sm-6" style="position:relative;margin-left:-15px">';
                        html+='<img src="<%=path%>/bm/images/help.png" />';
                        html+='<div class="col-sm-4 alert alert-success">短信默认70字符，含回复TD退订及签名字符，如选择活动，默认扣除19个短链字符！';
                        html+='</div>';
                    html+='</div>'; --%>
                html+='</div>';
                html+='<div class="form-group">';
                    html+='<label class="col-sm-2 control-label" style="visibility: hidden"></label>';
                    if(addFlag){
                        html+='<div rule_id="'+rule_id+'" class="col-sm-6 divcss5" style="border: 1px dashed #ddd;height: 30px; width: 400px;margin-left:15px">';

                    }else{
                        html+='<div hidden="" rule_id="'+rule_id+'" class="col-sm-6 divcss5" style="border: 1px dashed #ddd;height: 30px; width: 400px;margin-left:15px">';

                    }
                        html+='<i id="addVariables" class="fa fa-plus" style="margin-top:10px;margin-left:180px;"></i>';
                    html+='</div>';
                    
                html+='</div>';          
            html+='</form>';
         html+='</div>  ';
         
         
         $('#boxPage').append(html);
         $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});

        }
        function OKDialog1(title,content){
        	swal(
        		    title,
        		    content,
        		    'success'
        		  ).then(function () {
        			  window.location.href='<%=path%>/ser/ui/automatedOperation';
        			})
        }
        
        $(document).on('click', '.deleteRule', function() {
      	  $(this).parent().parent().parent().remove();
      	  
      	  var eachData=$('.eachData');
      	  var len=eachData.length-1;
      	  eachData.each(function(i,d){
	    	  if(i==len){
	    		  $(this).find('.divcss5').show();
	    	  }else{
	    		  $(this).find('.divcss5').hide();
	    	  }
	      		
	      });
      	  
		});
       
    </script>
    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
</body>
</html>
